<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();
    class Post_c extends CI_Controller {
        public function __construct() {
            parent::__construct();
            $this->load->model('post_m');
            $this->load->helper(array('form', 'url', 'html'));
            $this->load->library(array('form_validation', 'upload'));
            $this->load->library('session');
        }
        
        public function index() {
            if($this->session->userdata('role') === 'author'){
                $data['post'] = $this->post_m->get_all_my_post();
            }else{
                $data['post'] = $this->post_m->get_all_published_post();
            }
            $data['num_reject'] = $this->post_m->count_rejected_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post($this->session->userdata('user_id'));
            $data['num_trash'] = $this->post_m->count_trashed_post();
            $data['title'] = 'Post';
            $data['num_pending'] = $this->post_m->count_pending_post();
            $data['error'] = $this->session->flashdata('error');
            $data['success'] = $this->session->flashdata('success');
            
            $this->load->view('administrator/fragments/header_v', $data);
            $this->load->view('administrator/fragments/post_navigation_v', $data);
            $this->load->view('administrator/post_v', $data);
            $this->load->view('administrator/fragments/footer_v', $data);
        }
        
        function logout() {
            $this->session->unset_userdata('logged_in');
            session_destroy();
            redirect(base_url(), 'refresh');
        }
/*------------------------------------------------------------------------------------------------------*/
        function create_post() {
            $data['num_pending'] = $this->post_m->count_pending_post();
            $data['num_reject'] = $this->post_m->count_rejected_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post($this->session->userdata('user_id'));
            $data['num_trash'] = $this->post_m->count_trashed_post();
            $data['title'] = 'Create Post';
            $data['groups'] = $this->post_m->get_groups();
            $data['category'] = $this->post_m->get_category();
            
            $this->form_validation->set_rules('title', 'Title', 'required|xss_clean');
            /*$this->form_validation->set_rules('summary', 'Summary', 'required|xss_clean');*/
            $this->form_validation->set_rules('full_text', 'Full Text', 'required|xss_clean');
            $this->form_validation->set_rules('category', 'Category', 'required|xss_clean');
            /*$this->form_validation->set_rules('destination', 'Destination', 'required|xss_clean');*/
            
            if ($this->form_validation->run() === FALSE) {
                
                $this->load->view('administrator/fragments/header_v', $data);
                $this->load->view('administrator/fragments/post_navigation_v', $data);
                $this->load->view('administrator/post_create_v', $data);
                $this->load->view('administrator/fragments/footer_v', $data);
                
            } else {
                if(isset($_POST['save']) && !empty($_POST['save'])) {
                    /*$temp = explode(",", $this->input->post('destination'));*/
                    $result = $this->post_m->get_max_post();

                    $data = array(
                        'post_id' => $result['post_id'] + 1,
                        'title' => $this->input->post('title'),
                        'summary' => $this->input->post('summary'),
                        'full_text' => $this->input->post('full_text'),
                        'status' => 'draft',
                        'state' => 'show',
                        'author' => $this->session->userdata('user_id'),
                        'category_id' => $this->input->post('category'),
                    );
                    $this->post_m->save_post($data);
                    if(isset($_POST['bulletin'])){
                            $this->post_m->bulletin_post($data['post_id']);
                        }
                        foreach ($this->input->post('samp') as $group_id){
                            $this->post_m->gpost($data['post_id'],$group_id);
                        }
                    redirect(base_url().'post');
                } else if(isset($_POST['publish'])) {
                    if($this->session->userdata('role') === 'admin') {

                        $result = $this->post_m->get_max_post();

                        $data = array(
                            'post_id' => $result['post_id'] + 1,
                            'title' => $this->input->post('title'),
                            'summary' => $this->input->post('summary'),
                            'full_text' => $this->input->post('full_text'),
                            'status' => 'published',
                            'state' => 'show',
                            'author' => $this->session->userdata('user_id'),
                            'category_id' => $this->input->post('category'),
//                            'destination_id' => $this->input->post('destination'),
                        );
                        $this->post_m->publish_post($data);
                        if(isset($_POST['bulletin'])){
                            $this->post_m->bulletin_post($data['post_id']);
                        }
                        foreach ($this->input->post('samp') as $group_id){
                            $this->post_m->gpost($data['post_id'],$group_id);
                        }
                        
                        redirect(base_url().'post');
                    } else if($this->session->userdata('role') === 'author') {

                        $result = $this->post_m->get_max_post();

                        $data = array(
                            'post_id' => $result['post_id'] + 1,
                            'title' => $this->input->post('title'),
                            'summary' => $this->input->post('summary'),
                            'full_text' => $this->input->post('full_text'),
                            'status' => 'pending',
                            'author' => $this->session->userdata('user_id'),
                            'category_id' => $this->input->post('category'),
                        );

                        $this->post_m->publish_post($data);
                        if(isset($_POST['bulletin'])){
                            $this->post_m->bulletin_post($data['post_id']);
                        }
                        foreach ($this->input->post('samp') as $group_id){
                            $this->post_m->gpost($data['post_id'],$group_id);
                        }
                        redirect(base_url().'post');
                    }
                }
            }
        }
        
        
        function edit_post($post_id) {
            $data['num_pending'] = $this->post_m->count_pending_post();
            $data['num_reject'] = $this->post_m->count_rejected_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post($this->session->userdata('user_id'));
            $data['num_trash'] = $this->post_m->count_trashed_post();
            $post = $this->post_m->get_published_post($post_id);
            $data['title'] = 'Edit Post';
            
            $this->form_validation->set_rules('title', 'Title', 'required|xss_clean');
            $this->form_validation->set_rules('summary', 'Summary', 'required|xss_clean');
            $this->form_validation->set_rules('full_text', 'Full Text', 'required|xss_clean');
            $this->form_validation->set_rules('state', 'State', 'required|xss_clean');
            
            if(isset($_POST) && !empty($_POST)) {
                $data = array(
                    'title' => $this->input->post('title'),
                    'summary' => $this->input->post('summary'),
                    'full_text' => $this->input->post('full_text'),
                    'state' => $this->input->post('state')
                );
                
                if ($this->form_validation->run() === TRUE) {
                    
                    $this->post_m->update_post($post_id, $data);
                    
                    $this->session->set_flashdata('success', 'Post successfully updated.');
                    
                    redirect(base_url().'post');
                } 
                
            }
            
            $data['post'] = $post;
            
            $data['post_title'] = array(
                'name' => 'title',
                'id' => 'edit_post_title',
                'type' => 'text',
                'value' => $this->form_validation->set_value('title', $post['title'])
            );
            
            $data['summary'] = array(
                'name' => 'summary',
                'id' => 'edit_post_summary',
                'type' => 'text',
                'value' => $this->form_validation->set_value('summary', $post['summary'])
            );
            
            $data['full_text'] = array(
                'name' => 'full_text',
                'id' => 'edit_post_full_text',
                'class' => 'summary_editor',
                'type' => 'text',
                'value' => $this->form_validation->set_value('full_text', $post['full_text'])
            );
            
            $data['show'] = array(
                'name' => 'state',
                'id' => 'edit_post_show',
                'type' => 'radio',
                'checked' => ($this->form_validation->set_value('type', $post['state']) == 'show')? TRUE : FALSE,
                'value' => 'show'
            );
            
            $data['hide'] = array(
                'name' => 'state',
                'id' => 'edit_post_hide',
                'type' => 'radio',
                'checked' => ($this->form_validation->set_value('type', $post['state']) == 'hide')? TRUE : FALSE,
                'value' => 'hide'
            );
            
            $this->load->view('administrator/fragments/header_v', $data);
            $this->load->view('administrator/fragments/post_navigation_v', $data);
            $this->load->view('administrator/post_edit_v', $data);
            $this->load->view('administrator/fragments/footer_v', $data);  
        }
        
        function delete_post($post_id){
            $data['num_pending'] = $this->post_m->count_pending_post();
            $data['num_reject'] = $this->post_m->count_rejected_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post();
            $post = $this->post_m->get_trashed_post($post_id);
            
            $data = array(
                'title' => $post['title'],
                'summary' => $post['summary'],
                'full_text' => $post['full_text'],
                'date_time_pub' => $post['date_time_pub'],
                'date_time_modified' => $post['date_time_modified'],
                'date_time_deleted' => date('Y-m-d h:i:s'),
                'category_id' => $post['category_id'],
                'author' => $post['user_id'],
                'destination_id' => $post['destination_id'],
                'group_id' => $post['group_id'],
                'status' => $post['status'],
                'state' => 'hide'
            );
            $this->post_m->delete_post($post_id);
            
            $this->session->set_flashdata('success', 'Post successfully deleted.');
            
            redirect(base_url().'post');
        }
/*------------------------------------------------------------------------------------------------------*/
/*---------------------------------------------PENDING POST---------------------------------------------*/
/*------------------------------------------------------------------------------------------------------*/
        function show_pending_post() {
            $data['num_pending'] = $this->post_m->count_pending_post();
            $data['num_reject'] = $this->post_m->count_rejected_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post($this->session->userdata('user_id'));
            $data['num_trash'] = $this->post_m->count_trashed_post();
            $data['title'] = 'Pending Post';
            $data['pending'] = $this->post_m->get_all_pending_post();
            $data['error'] = $this->session->flashdata('error');
            $data['success'] = $this->session->flashdata('success');
            
            $this->load->view('administrator/fragments/header_v', $data);
            $this->load->view('administrator/fragments/post_navigation_v', $data);
            $this->load->view('administrator/post_pending_v', $data);
            $this->load->view('administrator/fragments/footer_v', $data);
        }
        
        function approve_pending_post_now($post_id) {
            $pending = $this->post_m->get_pending_post($post_id);
            
            $data = array(
                    'title' => $pending['title'],
                    'summary' => $pending['summary'],
                    'full_text' => $pending['full_text'],
                    'status' => 'published',
                    'state' => 'show'
                );
            
            $this->post_m->publish_pending_post($post_id, $data);
            
            $this->session->set_flashdata('success', 'Content Published');
            
            redirect(base_url().'post/pending/view');
            
        }
        
        function approve_pending_post($post_id) {
            $data['num_pending'] = $this->post_m->count_pending_post();
            $data['num_reject'] = $this->post_m->count_rejected_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post($this->session->userdata('user_id'));
            $data['num_trash'] = $this->post_m->count_trashed_post();
            $pending = $this->post_m->get_pending_post($post_id);
            $data['title'] = 'Approve Draft';
            
            $this->form_validation->set_rules('title', 'Title', 'required|xss_clean');
            $this->form_validation->set_rules('summary', 'Summary', 'required|xss_clean');
            $this->form_validation->set_rules('full_text', 'Full Text', 'required|xss_clean');
            
            if(isset($_POST) && !empty($_POST)) {
                $data = array(
                    'title' => $this->input->post('title'),
                    'summary' => $this->input->post('summary'),
                    'full_text' => $this->input->post('full_text'),
                    'status' => 'published',
                    'state' => 'show'
                );
                
                if ($this->form_validation->run() === TRUE) {
                    
                    $this->post_m->publish_pending_post($post_id, $data);
                    
                    $this->session->set_flashdata('success', 'Post approved.');
                    
                    redirect(base_url().'post/pending/view');
                } 
                
            }
            
            $data['pending'] = $pending;
            
            $data['pending_title'] = array(
                'name' => 'title',
                'id' => 'approve_pending_title',
                'type' => 'text',
                'readonly' => TRUE,
                'value' => $this->form_validation->set_value('title', $pending['title'])
            );
            
            $data['summary'] = array(
                'name' => 'summary',
                'id' => 'approve_pending_summary',
                'type' => 'text',
                'readonly' => TRUE,
                'value' => $this->form_validation->set_value('summary', $pending['summary'])
            );
            
            $data['full_text'] = array(
                'name' => 'full_text',
                'id' => 'approve_pending_full_text',
                'class' => 'summary_editor',
                'type' => 'text',
                'readonly' => TRUE,
                'value' => $this->form_validation->set_value('full_text', $pending['full_text'])
            );
            
            $this->load->view('administrator/fragments/header_v', $data);
            $this->load->view('administrator/post_pending_approve_v', $data);
            $this->load->view('administrator/fragments/footer_v', $data);
        }
        
        function reject_pending_post($post_id){
            $data['num_pending'] = $this->post_m->count_pending_post();
            $data['num_reject'] = $this->post_m->count_rejected_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post($this->session->userdata('user_id'));
            $data['num_trash'] = $this->post_m->count_trashed_post();
            $reject = $this->post_m->get_rejected_post($post_id);
            $data['title'] = 'Reject Draft';
            
            $this->form_validation->set_rules('title', 'Title', 'required|xss_clean');
            $this->form_validation->set_rules('full_text', 'Full Text', 'required|xss_clean');
            $this->form_validation->set_rules('comment', 'Comment', 'required|xss_clean');
            
            if(isset($_POST)&& !empty($_POST)){
                $data = $this->input->post('comment');
                
                if($this->form_validation->run() === TRUE) {
                    $this->post_m->insert_update_notification($post_id, $data);
                    
                    redirect(base_url().'post/pending/view');
                }
            }
            $data['rejected'] = $reject;
            
            $data['reject_title'] = array(
                'name' => 'title',
                'id' => 'reject_post_title',
                'readonly' => "readonly",
                'value' => $reject['title']
            );
            
            $data['full_text'] = array(
                'name' => 'full_text',
                'id' => 'reject_post_full_text',
                'readonly' => "readonly",
                'value' => $reject['full_text']
            );
            
            $data['comment'] = array(
                'name' => 'comment',
                'id' => 'reject_post_comment',
            );
            
            $this->load->view('administrator/fragments/header_v', $data);
            $this->load->view('administrator/post_pending_reject_v',$data);
            $this->load->view('administrator/fragments/footer_v', $data);
        }
/*------------------------------------------------------------------------------------------------------*/
/*---------------------------------------------DELETED POST---------------------------------------------*/
/*------------------------------------------------------------------------------------------------------*/        
        function post_trash() {
            $data['title'] = 'Deleted Post';
            $data['num_pending'] = $this->post_m->count_pending_post();
            $data['num_reject'] = $this->post_m->count_rejected_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post($this->session->userdata('user_id'));
            $data['num_trash'] = $this->post_m->count_trashed_post();
            $data['trash'] = $this->post_m->get_all_trashed_post();
            $data['message'] = $this->session->flashdata('message');
            
            $this->load->view('administrator/fragments/header_v', $data);
            $this->load->view('administrator/post_trash_v', $data);
            $this->load->view('administrator/fragments/footer_v', $data);
        }
        
        function recover_trashed_post($post_id) {
            $data = array(
                'date_time_modified' => date('Y-m-d h:i:s'),
                'status' => 'published'
            );
            $this->post_m->recover_trashed_post($post_id, $data);
            
            $this->session->set_flashdata('message', 'Post successfully recovered.');
            
            redirect(base_url().'post/trash');
        }
        
        function delete_trashed_post($trash_id) {
            $this->post_m->delete_trashed_post($trash_id);
            
            $this->session->set_flashdata('message', 'Post permanently deleted.');
            
            redirect(base_url().'post/trash');
        }
/*------------------------------------------------------------------------------------------------------*/
/*----------------------------------------------DRAFT POST----------------------------------------------*/
/*------------------------------------------------------------------------------------------------------*/
        function show_draft_post($author) {
            $data['num_pending'] = $this->post_m->count_pending_post();
            $data['num_reject'] = $this->post_m->count_rejected_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post($this->session->userdata('user_id'));
            $data['num_trash'] = $this->post_m->count_trashed_post();
            $data['title'] = 'Pending Post';
            $data['draft'] = $this->post_m->get_all_drafted_post($author);
            $data['error'] = $this->session->flashdata('error');
            $data['success'] = $this->session->flashdata('success');
            
            $this->load->view('administrator/fragments/header_v', $data);
            $this->load->view('administrator/fragments/post_navigation_v', $data);
            $this->load->view('administrator/post_draft_v', $data);
            $this->load->view('administrator/fragments/footer_v', $data);
        }
        
        function sort_draft_post($user, $order) {
//            $order = '';
//            if($o === TRUE) {
//                $order = '';
//            } else {
//                $order = 'DESC';
//            }
//            
//            $data['o'] = $o;
//            $data['u'] = $user;
            $data['num_pending'] = $this->post_m->count_pending_post();
            $data['num_reject'] = $this->post_m->count_rejected_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post($this->session->userdata('user_id'));
            $data['num_trash'] = $this->post_m->count_trashed_post();
            $data['title'] = 'Pending Post';
            $data['draft'] = $this->post_m->sort_draft($order, $user);
            $data['error'] = $this->session->flashdata('error');
            $data['success'] = $this->session->flashdata('success');
            
            $this->load->view('administrator/fragments/header_v', $data);
            $this->load->view('administrator/fragments/post_navigation_v', $data);
            $this->load->view('administrator/post_draft_v', $data);
            $this->load->view('administrator/fragments/footer_v', $data);
        }
        
        function edit_draft_post($post_id) {
            $data['num_pending'] = $this->post_m->count_pending_post();
            $data['num_reject'] = $this->post_m->count_rejected_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post($this->session->userdata('user_id'));
            $data['num_trash'] = $this->post_m->count_trashed_post();
            $draft = $this->post_m->get_drafted_post($post_id);
            $data['title'] = 'Edit Draft';
            
            $this->form_validation->set_rules('title', 'Title', 'required|xss_clean');
            $this->form_validation->set_rules('summary', 'Summary', 'required|xss_clean');
            $this->form_validation->set_rules('full_text', 'Full Text', 'required|xss_clean');
            if ($this->form_validation->run() === TRUE) {
                if(isset($_POST['save']) && !empty($_POST['save'])) {
                    $data = array(
                        'title' => $this->input->post('title'),
                        'summary' => $this->input->post('summary'),
                        'full_text' => $this->input->post('full_text'),
                        'status' => 'draft',
                        'state' => 'show'
                    );
                    $this->post_m->save_drafted_post($post_id, $data);
                    
                    $this->session->set_flashdata('message', 'Draft saved.');
                    
                    redirect('post/draft/'.$this->session->userdata('user_id'));
                }else if(isset ($_POST['publish']) && !empty ($_POST['publish'])) {
                    $data = array(
                        'title' => $this->input->post('title'),
                        'summary' => $this->input->post('summary'),
                        'full_text' => $this->input->post('full_text'),
                        'status' => 'published',
                        'state' => 'show'
                    );
                    $this->post_m->publish_drafted_post($post_id, $data);
                    $this->session->set_flashdata('message', 'Draft published.');
                    redirect('post/draft/'.$this->session->userdata('user_id'));
                }
            }   
            
            
            $data['draft'] = $draft;
            
            $data['draft_title'] = array(
                'name' => 'title',
                'id' => 'post_title',
                'type' => 'text',
                'value' => $this->form_validation->set_value('title', $draft['title'])
            );
            
            $data['summary'] = array(
                'name' => 'summary',
                /*'id' => 'approve_draft_summary',*/
                'type' => 'text',
                'value' => $this->form_validation->set_value('summary', $draft['summary'])
            );
            
            $data['full_text'] = array(
                'name' => 'full_text',
                'id' => 'post_full_text',
                'type' => 'text',
                'value' => $this->form_validation->set_value('full_text', $draft['full_text'])
            );
            
            $this->load->view('administrator/fragments/header_v', $data);
            $this->load->view('administrator/fragments/post_navigation_v', $data);
            $this->load->view('administrator/post_draft_edit_v', $data);
            $this->load->view('administrator/fragments/footer_v', $data);
        }
        
        function publish_draft_post($post_id) {
            $draft = $this->post_m->get_drafted_post($post_id);
            
            $data = array(
                        'title' => $draft['title'],
                        'summary' => $draft['summary'],
                        'full_text' => $draft['full_text'],
                        'status' => 'published',
                        'state' => 'show'
                    );
                    $this->post_m->publish_drafted_post($post_id, $data);
                    $this->session->set_flashdata('message', 'Draft published.');
                    redirect('post/draft/'.$this->session->userdata('user_id'));
        }
        
        function delete_draft_post($post_id) {
            $this->post_m->delete_drafted_post($post_id);
            
            $this->session->set_flashdata('message', 'Draft permanently deleted.');
            
            redirect(base_url().'post/draft/'.$this->session->userdata('user_id'));
        } 
/*------------------------------------------------------------------------------------------------------*/
/*--------------------------------------------REJECTED POST---------------------------------------------*/
/*------------------------------------------------------------------------------------------------------*/
        function show_rejected_post() {
            $data['title'] = 'Pending Post';
            $data['num_pending'] = $this->post_m->count_pending_post();
            $data['num_reject'] = $this->post_m->count_rejected_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post($this->session->userdata('user_id'));
            $data['num_trash'] = $this->post_m->count_trashed_post();
            $data['rejected'] = $this->post_m->get_all_rejected_post($this->session->userdata('user_id'));
            $data['message'] = $this->session->flashdata('message');
            
            $this->load->view('administrator/fragments/header_v', $data);
            $this->load->view('administrator/fragments/post_navigation_v', $data);
            $this->load->view('administrator/post_reject_v', $data);
            $this->load->view('administrator/fragments/footer_v', $data);
        }
        
        function reject_edit_post($post_id) {
            $data['num_pending'] = $this->post_m->count_pending_post();
            $data['num_reject'] = $this->post_m->count_rejected_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post($this->session->userdata('user_id'));
            $data['num_trash'] = $this->post_m->count_trashed_post();
            $rejected = $this->post_m->get_rejected_post($post_id);
            $data['title'] = 'Edit Post';
            
            $this->form_validation->set_rules('title', 'Title', 'required|xss_clean');
            $this->form_validation->set_rules('summary', 'Summary', 'required|xss_clean');
            $this->form_validation->set_rules('full_text', 'Full Text', 'required|xss_clean');
            
            if(isset($_POST) && !empty($_POST)) {
                $data = array(
                    'title' => $this->input->post('title'),
                    'summary' => $this->input->post('summary'),
                    'full_text' => $this->input->post('full_text'),
                    'status' => 'pending',
                    'state' => 'show'
                );
                
                if ($this->form_validation->run() === TRUE) {
                    
                    $this->post_m->publish_pending_post($post_id, $data);
                    
                    $this->session->set_flashdata('message', 'Post re-published successfully.');
                    
                    redirect(base_url().'post/rejected');
                } 
                
            }
            
            $data['rejected'] = $rejected;
            
            $data['rejected_title'] = array(
                'name' => 'title',
                'id' => 'rejected_title',
                'type' => 'text',
                'readonly' => TRUE,
                'value' => $this->form_validation->set_value('title', $rejected['title'])
            );
            
            $data['summary'] = array(
                'name' => 'summary',
                'id' => 'rejected_summary',
                'type' => 'text',
                'readonly' => TRUE,
                'value' => $this->form_validation->set_value('summary', $rejected['summary'])
            );
            
            $data['full_text'] = array(
                'name' => 'full_text',
                'id' => 'rejected_full_text',
                'class' => 'summary_editor',
                'type' => 'text',
                'readonly' => TRUE,
                'value' => $this->form_validation->set_value('full_text', $rejected['full_text'])
            );
            
            $this->load->view('administrator/fragments/header_v', $data);
            $this->load->view('administrator/post_reject_edit_v', $data);
            $this->load->view('administrator/fragments/footer_v', $data);
        }
    }
?>
